package com.example.opengl.camera.dialog

import android.content.Context
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.example.opengl.R

class FilterAdapter(private val context: Context, private val filterBeans: List<FilterBean>?) :
    RecyclerView.Adapter<FilterAdapter.ViewHolder>() {
    private val TAG = PagerAdapter::class.java.simpleName
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.shortvideo_record_filter_item, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val filterBean = filterBeans!![position]
        holder.ivFilterview.setImageResource(filterBean.filterImageId)
        holder.tvFilterview.text = filterBean.filterName
        if (filterBean.isSelect) {
            holder.rlFilterviewBg.setBackgroundResource(R.drawable.shape_bg_r2_ff6abbe1)
            holder.tvFilterview.setTextColor(context.resources.getColor(com.android.baselibrary.R.color.primary_blue))
            holder.tvFilterview.typeface = Typeface.DEFAULT_BOLD
            //            holder.viewSelect.setVisibility(View.VISIBLE);
        } else {
//            holder.rlFilterviewBg.setBackgroundResource(R.drawable.shape_bg_r2_ff1e1e1e);
            holder.rlFilterviewBg.background = null
            holder.tvFilterview.setTextColor(context.resources.getColor(com.android.baselibrary.R.color.common_color_bbbbbb))
            holder.tvFilterview.typeface = Typeface.DEFAULT
            //            holder.viewSelect.setVisibility(View.GONE);
        }
        holder.itemView.tag = position
        holder.itemView.setOnClickListener { v ->
            val pos = v.tag as Int
            for (i in filterBeans.indices) {
                if (i != pos) {
                    filterBeans[i].isSelect = false
                } else {
                    filterBeans[i].isSelect = true
                }
            }
            if (filterItemSelectListener != null) {
                filterItemSelectListener!!.filterItemSelect(filterBeans[pos], pos)
            }
            notifyDataSetChanged()
        }
    }

    override fun getItemCount(): Int {
        return filterBeans?.size ?: 0
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var ivFilterview: ImageView
        var tvFilterview: TextView
        var viewSelect: View
        var rlFilterviewBg: RelativeLayout

        init {
            rlFilterviewBg = itemView.findViewById(R.id.rl_filterview_bg)
            ivFilterview = itemView.findViewById(R.id.iv_filterview)
            tvFilterview = itemView.findViewById(R.id.tv_filterview)
            viewSelect = itemView.findViewById(R.id.view_select)
        }
    }

    interface FilterItemSelectListener {
        fun filterItemSelect(filterBean: FilterBean?, pos: Int)
    }

    var filterItemSelectListener: FilterItemSelectListener? = null
    @JvmName("setFilterOnClick")
    fun setFilterItemSelectListener(filterItemSelectListener: FilterItemSelectListener?) {
        this.filterItemSelectListener = filterItemSelectListener
    }
}